﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using BLL;
using Model;
using System.Threading;
using Newtonsoft.Json;

namespace Sales
{
    public partial class frmLogin : Form
    {
        private LoginBLL loginBLL;
        private yyydaHModel yyydaHModel;
        //定义闪屏对象

        public frmLogin()
        {
            InitializeComponent();

            this.cLabel1.Font = new Font("宋体", 15, FontStyle.Bold);
            this.cLabel2.Font = new Font("宋体", 15, FontStyle.Bold);

            //this.pbtnLogin.Font = new Font("宋体", 12, FontStyle.Bold);
            //this.pbtnQuit.Font = new Font("宋体", 12, FontStyle.Bold);


        }

        private void frmLogin_Load(object sender, EventArgs e)
        {
            LoadDrop();

        }

        private void LoadDrop()
        {
            string error = "";
            loginBLL = new LoginBLL();
            DataTable dt = loginBLL.GetyyydaHbyGh(ref error);

            if (dt != null && dt.Rows.Count > 0)
            {
                this.comGh.DataSource = dt;
                this.comGh.DisplayMember = "ygbh";
                this.comGh.ValueMember = "ygbh";

            }
            this.label1.Text = error;
        }
        private void GetYyyDa()
        {
            ProgressForm progressForm = new ProgressForm();
            progressForm.ShowDialog();
        }

        private void ctxtPwd_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 13)
            {
                LoginIn();
            }
        }

        private void frmLogin_Activated(object sender, EventArgs e)
        {
            //设置光标位置
            this.ctxtPwd.Focus();
        }

        private void imageButton2_Click(object sender, EventArgs e)
        {
            frmSetDB setDB = new frmSetDB();
            setDB.ShowDialog();
            LoadDrop();

        }

        private void LoginIn()
        {
            loginBLL = new LoginBLL();
            yyydaHModel = new yyydaHModel();
            try
            {
                string ygbh = "";
                string dlmm = "";

                ygbh = this.comGh.Text;
                dlmm = this.ctxtPwd.Text.Trim();

                if (dlmm != "")
                {
                    yyydaHModel = loginBLL.GetUserInfo(ygbh, dlmm);
                    if (yyydaHModel != null)
                    {

                        frmMain main = frmMain.CreateInstance();

                        int pw = 0;
                        if (yyydaHModel.xl != "" && int.Parse(yyydaHModel.xl) > 100)
                            pw = 1;
                        LoginInfo model = new LoginInfo();
                        model.Syybh = yyydaHModel.ygbh;
                        model.Syymc = yyydaHModel.ymmc;

                        model.Yyybh = yyydaHModel.ygbh;
                        model.Yyymc = yyydaHModel.ymmc;

                        model.Dmbh = yyydaHModel.dmbh;
                        model.Dmmc = yyydaHModel.dmmc;

                        model.Power = pw;
                        model.Zdr = yyydaHModel.ymmc;

                        string jsonGroup = JsonConvert.SerializeObject(model);

                        MiddleModule.SendMessage(this, main, jsonGroup);
                        this.Hide();
                        main.Show();
                    }
                    else
                    {
                        Message.MsgBoxWarn("用户名或密码有错");
                    }
                }
                else
                {
                    Message.Warning("密码为空不允许登录！");
                }

            }
            catch (Exception ex)
            {
                Message.Warning("登录失败1！" + ex.Message);
            }
        }

        private void csbtnLoginIn_Click(object sender, EventArgs e)
        {
            LoginIn();
        }

        private void csbtnCancel_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
    }
}
